<?php

/*
 * Modulo de Usuarios
 * Sergio Sam scs.1975@gmail.com
*/

class Users extends Controller {

    function Users() {
        parent::Controller();
        $this->load->library('session');
        $this->load->language('general',$this->session->userdata('lenguaje'));
    }

    function index() {
        $id = $this->session->userdata('id');
        $datos['titulo_pagina'] = 'Datos Personales';
        $this->load->view('encabezado', $datos);
        $this->load->model('admin/modelo_modulos');
        $this->modelo_modulos->get_modulos();

        //Calendario de Vencimientos
        $ano = $this->uri->segment(3);
        $mes = $this->uri->segment(4);
        $this->load->model('vencimientos_empresa1/modelo_vencimientos');
        $this->modelo_vencimientos->get_vencimientos($ano,$mes);
        //Fin calendario

        $this->load->view('menu');
        $consulta['query'] = $this->db->query("SELECT * FROM users WHERE id=".$id);
        $fila = $consulta['query']->row();
        $this->load->view('am_datospersonales',$consulta);
        $this->load->view('pie');
    }

    function login() {
        //Load
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $rules['email_r']           = "required";
        $rules['login_password']    = "required";

        $this->validation->set_rules($rules);
        $fields['email_r']          = 'Email';
        $fields['login_password']   = 'Clave';
        $fields['email']  = 'Email Address';

        $this->validation->set_fields($fields);

        $this->validation->set_message('required', 'Su cuenta de mail y su Clave son requeridas');
        $this->validation->set_error_delimiters('<div style="color:red">', '</div>');

        if ($this->validation->run() == FALSE) {

            $datos['titulo_pagina'] = lang('titulo');
            $this->load->helper('url');

            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');

            $fields['email_r'] = 'Email';
            $fields['login_password'] = 'Passord';
            $this->validation->set_fields($fields);

            $this->load->view('encabezado', $datos);
            $ano = $this->uri->segment(3);
            $mes = $this->uri->segment(4);
            $this->load->model('vencimientos_empresa1/modelo_vencimientos');
            $this->modelo_vencimientos->get_vencimientos($ano,$mes);

            $this->load->model('admin/modelo_modulos');
            $this->modelo_modulos->get_modulos();

            $this->load->view('menu');

            $this->load->model('home/home_model');
            $this->load->model('noticias/modelo_noticias');
            $this->load->view('home/contenido');
            $this->load->view('pie');

        }  else {

            if($this->simplelogin->login($this->input->post('email_r'), $this->input->post('login_password'), $this->input->post('letra'), $this->input->post('numero'), $this->input->post('login_tarjeta') )) {
                redirect('/home/');
            }else {
                $datos['titulo_pagina'] = lang('titulo');
                $this->load->helper('url');

                $this->load->helper(array('form', 'url'));
                $this->load->library('validation');

                $fields['email_r'] = 'Email';
                $fields['login_password'] = 'Passord';
                $this->validation->set_fields($fields);

                $this->load->view('encabezado', $datos);
                $ano = $this->uri->segment(3);
                $mes = $this->uri->segment(4);
                $this->load->model('vencimientos_empresa1/modelo_vencimientos');
                $this->modelo_vencimientos->get_vencimientos($ano,$mes);

                $this->load->model('admin/modelo_modulos');
                $this->modelo_modulos->get_modulos();

                $dd['error']="Clave o nombre de usuario incorrectos!";
                $this->load->view('menu',$dd);

                $this->load->model('home/home_model');
                $this->load->model('noticias/modelo_noticias');
                $this->load->view('home/contenido');
                $this->load->view('pie');

            }

        }

    }

    function logout() {
        //Load
        $this->load->helper('url');

        //Logout
        $this->simplelogin->logout();
        redirect('/home/');
    }

    function modificar() {
        $id = $this->input->post('id');
        $username  = $this->input->post('create_username');
        $password  = $this->input->post('create_password');
        $mail      = $this->input->post('mail');
        $direccion = $this->input->post('address_1');
        $telefono  = $this->input->post('phone_1');
        if ($password) {
            $query = $this->db->query("UPDATE users set password='".md5($password)."', email='".$mail."', adress_1='".$direccion."', phone_1='".$telefono."' WHERE id=".$id);
        }else {
            $query = $this->db->query("UPDATE users set email='".$mail."', address_1='".$direccion."', phone_1='".$telefono."' WHERE id=".$id);
        }
        redirect('/home/');
    }

}
?>